﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using myMvcBlog.Models;
using SubSonic.Schema;
using System.Data;
using Joel.Net;
using myMvcBlog.Services;

namespace myMvcBlog.Controllers
{
    public class HomeController : BaseController
    {
        public ActionResult HomePaging(int page)
        {
            HomeView homeView = new HomeView();
            homeView.Posts = Post.All().OrderByDescending(post => post.PostedTime)
                                .Skip((page) * ItemsPerPage).Take(ItemsPerPage).ToList();
            PagerView pager = new PagerView();
            pager.PageCount = (int)Math.Ceiling((double)Post.All().Count() / ItemsPerPage);
            pager.CurrentPage = page;
            pager.ActionName = "HomePaging";
            pager.ControllerName = "Home";

            homeView.PagerView = pager;

            return View("Index",homeView);
        }

        public ActionResult Index()
        {
            HomeView homeView = new HomeView();
            homeView.Posts = Post.All().OrderByDescending(post => post.PostedTime)
                                .Take(ItemsPerPage).ToList();
            PagerView pager = new PagerView();
            pager.PageCount = (int)Math.Ceiling((double)Post.All().Count() / ItemsPerPage);
            pager.CurrentPage = 0;
            pager.ActionName = "HomePaging";
            pager.ControllerName = "Home";

            homeView.PagerView = pager;

            return View(homeView);
        }


        public new ActionResult View(string niceName)
        {
            Post post = Post.SingleOrDefault(p => p.UrlName == niceName);

            return View(post);
        }

        //public ActionResult Comment(long postId, string author, string email, string url, string comment)
        //{
        //    Comment cmt = new Comment()
        //    {
        //        Author = author,
        //        Email = email,
        //        IP = Request.UserHostAddress,
        //        Site = url,
        //        CommentTime = DateTime.Now,
        //        Content = comment,
        //        PostID = postId
        //    };

        //    cmt.IsSpam = !SpamCommentService.CheckComment(cmt);

        //    myMvcBlog.Comment.GetRepo().Add(cmt);

        //    return RedirectToAction("View", new { nicename = Post.SingleOrDefault(p => p.ID == postId).UrlName });
        //}

        public ActionResult About()
        {
            return View();
        }

        [ChildActionOnly]
        public ActionResult Sidebar()
        {
            SidebarView sidebar = new SidebarView();
            sidebar.Categories = myMvcBlog.Category.All().ToList();
            sidebar.RecentPosts = Post.All().OrderByDescending(post => post.PostedTime).Take(ItemsPerPage).ToList();
            sidebar.RecentComments = myMvcBlog.Comment.All().OrderByDescending(c => c.CommentTime).Take(ItemsPerPage).ToList();

            IDataReader monthlyArchiveReader = new StoredProcedure("dbo.GetMonthlyArchive").ExecuteReader();

            IList<MonthlyArchive> archives = new List<MonthlyArchive>();

            while (monthlyArchiveReader.Read())
            {
                archives.Add(
                        new MonthlyArchive()
                        {
                            Year = monthlyArchiveReader["Year"].ToString(),
                            Month = monthlyArchiveReader["Month"].ToString(),
                            Count = monthlyArchiveReader["Count"].ToString()
                        }
                    );
            }

            sidebar.MonthlyArchives = archives;

            return PartialView("_SidebarPartial", sidebar);
        }
    }
}
